const Koa = require('koa');
const Router = require('koa-Router');
const bodyparser = require('koa-bodyparser');
const cors = require('@koa/cors');
const path = require('path');
const fs = require('fs');
const Mock = require('mockjs');
const { WebSocketServer } = require('ws');
const wss = new WebSocketServer({ port: 8080 });

const data = Mock.mock({
    "list|10": [
        {
            'key|+1': 1,
            name: "@name",
            'age|16-60': 1,
            city: '@city',
            phone: /^1[3-9]\d{9}/,
            time: '@date'
        }
    ],
    "echarts|10": [
        {
            'year|+1': 1991,
            'value|10-100': 1
        }
    ]
})

const userPathFile = path.join(__dirname, './data', '/user.json');

const app = new Koa();
const router = new Router();

app.use(bodyparser());
app.use(cors());

router.post('/api/login', ctx => {
    const { username, password } = ctx.request.body;
    console.log(username, password);

    const userStr = fs.readFileSync(userPathFile, 'utf-8');
    const userArr = JSON.parse(userStr);

    const flag = userArr.some(item => item.username === username && item.password === password);
    if (!flag) {
        ctx.body = {
            code: 401,
            message: "账号或密码错误"
        }
        return
    }

    ctx.body = {
        code: 200,
        message: "登录成功"
    }
})

router.get('/api/data', ctx => {
    ctx.body = {
        code: 200,
        message: "数据获取成功",
        data: data
    }
})

wss.on('connection', function connection(ws) {
    ws.on('error', console.error);

    ws.on('message', function message(data) {
        console.log('received: %s', data);
    });

    const init = setInterval(() => {
        const real = Mock.mock({
            'list': [
                {
                    koa: [
                        { year: '1991', 'value|1-100': 1 },
                        { year: '1992', value: Mock.mock('@integer(1,100)') },
                        { year: '1993', value: Mock.mock('@integer(1,100)') },
                        { year: '1994', value: Mock.mock('@integer(1,100)') },
                        { year: '1995', value: Mock.mock('@integer(1,100)') },
                        { year: '1996', value: Mock.mock('@integer(1,100)') },
                        { year: '1997', value: Mock.mock('@integer(1,100)') },
                        { year: '1998', value: Mock.mock('@integer(1,100)') },
                        { year: '1999', value: Mock.mock('@integer(1,100)') },
                    ]
                }
            ]
        })
        ws.send(JSON.stringify(real));
    }, 3000)

    ws.send(JSON.stringify(Mock.mock({
        'list': [
            {
                koa: [
                    { year: '1991', value: Mock.mock('@integer(1,100)') },
                    { year: '1992', value: Mock.mock('@integer(1,100)') },
                    { year: '1993', value: Mock.mock('@integer(1,100)') },
                    { year: '1994', value: Mock.mock('@integer(1,100)') },
                    { year: '1995', value: Mock.mock('@integer(1,100)') },
                    { year: '1996', value: Mock.mock('@integer(1,100)') },
                    { year: '1997', value: Mock.mock('@integer(1,100)') },
                    { year: '1998', value: Mock.mock('@integer(1,100)') },
                    { year: '1999', value: Mock.mock('@integer(1,100)') },
                ]
            }
        ]
    })))
});

app.use(router);

app.listen(9000, () => {
    console.log('服务器启动成功');
})